package com.yzh.openai.domain.openai.service.rule.impl;

import com.yzh.openai.domain.openai.annotation.LogicStrategy;
import com.yzh.openai.domain.openai.model.aggregates.ChatProcessAggregate;
import com.yzh.openai.domain.openai.model.entity.RuleLogicEntity;
import com.yzh.openai.domain.openai.model.entity.UserAccountEntity;
import com.yzh.openai.domain.openai.model.valobj.LogicCheckTypeVO;
import com.yzh.openai.domain.openai.service.rule.ILogicFilter;
import com.yzh.openai.domain.openai.service.rule.factory.DefaultLogicFactory;
import com.yzh.openai.types.enums.AIModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 允许访问的模型过滤
 */
@Slf4j
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.MODEL_TYPE)
public class ModelTypeFilter implements ILogicFilter<UserAccountEntity> {
    @Override
    public RuleLogicEntity<ChatProcessAggregate> filter(ChatProcessAggregate chatProcess, UserAccountEntity data) throws ExecutionException {
        //1.用户可用模型
        List<String> allowModelTypeList = data.getAllowModelTypeList();
        String model = chatProcess.getModel().getCode();

        //2.模型校验通过
        if (allowModelTypeList.contains(model) || allowModelTypeList.contains(AIModel.ALL_MODEL.getCode())) {
            return RuleLogicEntity.<ChatProcessAggregate>builder()
                    .type(LogicCheckTypeVO.SUCCESS)
                    .data(chatProcess)
                    .build();
        }

        //3.模型校验拦截
        return RuleLogicEntity.<ChatProcessAggregate>builder()
                .type(LogicCheckTypeVO.REFUSE)
                .info("当前账户不支持【" + model + "】模型！请购买后使用")
                .build();
    }
}
